package com.ziyun.erp.web.api.goodsCraftPrice;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.utility.JsonUtils;
import com.ziyun.erp.modules.goods.entity.GoodsTemplateEntity;
import com.ziyun.erp.modules.goods.service.GoodsAttributeService;
import com.ziyun.erp.modules.goods.service.GoodsOptionService;
import com.ziyun.erp.modules.goods.service.GoodsTemplateService;
import com.ziyun.erp.modules.goodsCraftPrice.dto.GoodsTemplateOptionDto;
import com.ziyun.erp.modules.goodsCraftPrice.entity.GoodsCraftPriceEntity;
import com.ziyun.erp.modules.goodsCraftPrice.service.GoodsCraftPriceService;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 标品工艺价格表
 *
 * @author zh
 * @email 296588519@qq.com
 * @date 2018-09-10 10:43:12
 */
@RestController
@RequestMapping("port/goodsCraftPrice/goodscraftprice")
public class GoodsCraftPriceApi {
    @Autowired
    private GoodsCraftPriceService goodsCraftPriceService;

    @Autowired
    private GoodsAttributeService goodsAttributeService;

    @Autowired
    private GoodsOptionService goodsOptionService;

    @Autowired
    private GoodsTemplateService goodsTemplateService;


    /**
     * 获取基础商品工艺价格
     */
    @RequestMapping("/getGoodsCraftPriceList")
    public R getGoodsCraftPriceList(@RequestBody Map paramBodyMap) {
        //商户id
        Long sellerId = Long.valueOf(paramBodyMap.get("sellerId").toString());

        Map<String, Object> goodsCraftPriceData = goodsCraftPriceService.getGoodsCraftPriceList(sellerId);

        return R.ok().put("goodsCraftPriceData", goodsCraftPriceData);
    }

    /**
     * 获取基础商品模板工艺价格
     */
    @RequestMapping("/getGoodsTemplateCraftPriceList")
    public R getGoodsTemplateCraftPriceList(@RequestBody Map paramBodyMap){
        //商户id
        Long sellerId = Long.valueOf(paramBodyMap.get("sellerId").toString());

        Map<String ,Object> goodsCraftPriceData = goodsCraftPriceService.getGoodsTemplateCraftPriceList(sellerId);

        return R.ok().put("goodsCraftPriceData", goodsCraftPriceData);
    }

    /**
     * 获取基础商品模板
     */
    @RequestMapping("/getGoodsTemplateList")
    public R getGoodsTemplateList(@RequestBody Map paramBodyMap){

        Integer attributeId = Integer.parseInt(paramBodyMap.get("attributeId").toString());

        List<GoodsTemplateEntity> goodsTemplateList = this.goodsTemplateService.selectGoodsTemplateList(attributeId);

        return R.ok().put("goodsTemplateList", goodsTemplateList);
    }

    /**
     * 获取商品模板 关联的选项
     * @param goodsTemplateOptionDto
     * @return
     */
    @RequestMapping("/getGoodsTemplateOptionList")
    public R getGoodsTemplateOptionList(@RequestBody GoodsTemplateOptionDto goodsTemplateOptionDto){

        List<GoodsCraftPriceEntity> goodsCraftOptionPriceList = this.goodsCraftPriceService.getGoodsTemplateOptionList(goodsTemplateOptionDto);

        return R.ok().put("goodsCraftOptionPriceList", goodsCraftOptionPriceList);
    }


    /**
     * 获取基础商品工艺
     */
    @RequestMapping("/getGoodsCraftList")
    public R getGoodsCraftList() {

        List<Map<String, Object>> goodsCraftList = goodsAttributeService.getGoodsCraftList();

        return R.ok().put("goodsCraftList", goodsCraftList);
    }

    /**
     * 获取基础商品工艺选项
     */
    @RequestMapping("/getGoodsCraftOptionList")
    public R getGoodsCraftOptionList(@RequestBody Map paramBodyMap) {

        List<GoodsCraftPriceEntity> goodsCraftOptionPriceList = goodsCraftPriceService.getGoodsCraftOptionPriceList(paramBodyMap);


        return R.ok().put("goodsCraftOptionPriceList", goodsCraftOptionPriceList);
    }


    /**
     * 保存基础商品工艺价格
     */
    @RequestMapping("/save")
    public R save(@RequestBody Map paramBodyMap) {
        // 获取当前登录人
        String userName = paramBodyMap.get("userName").toString();
        //商户id
        Long sellerId = Long.valueOf(paramBodyMap.get("sellerId").toString());

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);


        List<GoodsCraftPriceEntity> craftPriceList = JsonUtils.string2Collection(paramBodyMap.get("craftPriceList").toString(),List.class, GoodsCraftPriceEntity.class) ;
        goodsCraftPriceService.save(craftPriceList, userName, sellerId);
        return R.ok();
    }


    /**
     * 根据工艺删除
     */
    @RequestMapping("/deleteByAttributeId")
    public R deleteByAttributeId(@RequestBody Map paramBodyMap) {
        goodsCraftPriceService.deleteByAttributeId(paramBodyMap);
        return R.ok();
    }

    /**
     * 根据工艺陪价删除
     */
    @RequestMapping("/deleteByIds")
    public R deleteByIds(@RequestBody Map paramBodyMap) {

        List<Integer> ids = JsonUtils.string2Collection(paramBodyMap.get("ids").toString(), List.class, Integer.class);

        goodsCraftPriceService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 获取基础商品模板工艺价格 弹窗用
     */
    @RequestMapping("/getGoodsTemplateCraftPriceWindowList")
    public R getGoodsTemplateCraftPriceWindowList(@RequestBody Map paramBodyMap){
        Map<String ,Object> goodsCraftPriceData = goodsCraftPriceService.getGoodsTemplateCraftPriceWindowList(paramBodyMap);
        return R.ok().put("goodsCraftPriceData", goodsCraftPriceData);
    }

}
